<?php
$data = array(); //array set untuk insert into DB
        
//susun dulu jadi per parent, ada cara yang lebih baik silakan diubah aja ^_^
foreach (YII::app()->session['layanan_hd'] as $k => $v) {
    foreach ($v as $kk => $vv) {
        $data[] = array('pid' => $k, 'id' => $kk, "text" => $vv);
    }
}

?>

<div id="tree-pelayanan"></div>
<script>
    
    $(function() {
        var data = <?php echo json_encode($data); ?>;
        var builddata = function (data) {
            var source = [];
            var items = [];
            // build hierarchical source.
            for (i = 0; i < data.length; i++) {
                var item = data[i];
                var label = item["text"];
                var parentid = item["pid"];
                var id = item["id"];

                if (items[parentid]) {
                    var item = { parentid: parentid, label: label, item: item, index:i };
                    if (!items[parentid].items) {
                        items[parentid].items = [];
                    }
                    items[parentid].items[items[parentid].items.length] = item;
                    items[id] = item;
                }
                else {
                    items[id] = { parentid: parentid, label: label, item: item, index:i };
                    source[id] = items[id];
                }
            }
            return source;
        }
        var source = builddata(data);
        
        $('#tree-pelayanan').jqxTree({ source: source, width: '250px' });
    });
</script>